﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XChipTool.Enties;
using XChip_Common;

namespace XChipTool
{
    public partial class XChip_ADCPollingForm : DevExpress.XtraEditors.XtraForm
    {
        private List<BinChoiceData> listC = new List<BinChoiceData>();
        private bool isSaveMask = false;//保存标记
        //public delegate void SendADCPollingClick(bool isAdc);

        //public event SendADCPollingClick SendADCClick;


        public XChip_ADCPollingForm(List<BinChoiceData> _list)
        {
            InitializeComponent();
            cmb_Setting0.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DisplayMember"));
            cmb_Setting1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DisplayMember"));
            cmb_Setting2.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DisplayMember"));
            cmb_Setting3.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DisplayMember"));
            cmb_Setting0.Properties.ValueMember = "ValueMember";
            cmb_Setting1.Properties.ValueMember = "ValueMember";
            cmb_Setting2.Properties.ValueMember = "ValueMember";
            cmb_Setting3.Properties.ValueMember = "ValueMember";
            cmb_Setting0.Properties.DisplayMember = "DisplayMember";
            cmb_Setting1.Properties.DisplayMember = "DisplayMember";
            cmb_Setting2.Properties.DisplayMember = "DisplayMember";
            cmb_Setting3.Properties.DisplayMember = "DisplayMember";
            cmb_Setting0.Properties.ShowHeader = false;
            cmb_Setting1.Properties.ShowHeader = false;
            cmb_Setting2.Properties.ShowHeader = false;
            cmb_Setting3.Properties.ShowHeader = false;
            listC = _list;
        }

        private void XChip_ADCPollingForm_Load(object sender, EventArgs e)
        {
            List<TreeListValue> cmbDS = new List<TreeListValue>();
            for (int i = 0; i < listC.Count; i++)
            {
                cmbDS.Add(new TreeListValue { ValueMember = listC[i].tag.ToString(), DisplayMember = listC[i].BinName });
                listC[i].Sort = i;
                listC[i].isTick = false;
                listC[i].isDel = false;
            }
            cmb_Setting0.Properties.DataSource = cmbDS;
            cmb_Setting1.Properties.DataSource = cmbDS;
            cmb_Setting2.Properties.DataSource = cmbDS;
            cmb_Setting3.Properties.DataSource = cmbDS;

            if (ParaHelp.Bin_ADC.Where((c, i) => c.SaveMask && i >= 11).Count() == 4)//假如bin_ADC均保存过，则恢复上次数据
            {
                string temp0 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c17").PValue, 16).ToString();
                string temp1 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c18").PValue, 16).ToString();
                string temp2 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c19").PValue, 16).ToString();
                string temp3 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c1a").PValue, 16).ToString();
                cmb_Setting0.EditValue = temp0;
                cmb_Setting1.EditValue = temp1;
                cmb_Setting2.EditValue = temp2;
                cmb_Setting3.EditValue = temp3;
            }

           
        }

        private void XChip_ADCPollingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isSaveMask)//假如未保存
            {
                var result = XtraMessageBox.Show("未保存,是否保存？", "保存提示", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (result == DialogResult.Yes && !SaveADCPollingSetting())
                {
                    e.Cancel = true;
                }
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            isSaveMask = SaveADCPollingSetting();
        }
        private bool SaveADCPollingSetting()
        {
            #region 判断是否存在未选择的下拉框
            if(cmb_Setting0.EditValue == null || cmb_Setting1.EditValue == null || cmb_Setting2.EditValue == null || cmb_Setting3.EditValue == null)
            {
                XtraMessageBox.Show("有下拉框未选择！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            #endregion
            try
            {
                var list = listC.Where(c => c.isDel == false).OrderBy(c => c.Sort).ToList();

                ParaHelp.Bin_ADC.Find(c=>  c.PKey == "0x0c17").PValue = $"0x{Convert.ToInt16(cmb_Setting0.EditValue).ToString("x2").PadLeft(2, '0')}";
                ParaHelp.Bin_ADC.Find(c => c.PKey == "0x0c18").PValue = $"0x{Convert.ToInt16(cmb_Setting1.EditValue).ToString("x2").PadLeft(2, '0')}";
                ParaHelp.Bin_ADC.Find(c => c.PKey == "0x0c19").PValue = $"0x{Convert.ToInt16(cmb_Setting2.EditValue).ToString("x2").PadLeft(2, '0')}";
                ParaHelp.Bin_ADC.Find(c => c.PKey == "0x0c1a").PValue = $"0x{Convert.ToInt16(cmb_Setting3.EditValue).ToString("x2").PadLeft(2, '0')}";
                ParaHelp.Bin_ADC.Find(c => c.PKey == "0x0c17").SaveMask = true;
                ParaHelp.Bin_ADC.Find(c => c.PKey == "0x0c18").SaveMask = true;
                ParaHelp.Bin_ADC.Find(c => c.PKey == "0x0c19").SaveMask = true;
                ParaHelp.Bin_ADC.Find(c => c.PKey == "0x0c1a").SaveMask = true;
                foreach (var item in ParaHelp.Bin_ADC)
                {
                    item.SaveMask = true;
                }
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！");
                //if (SendADCClick!=null)
                //{
                //    SendADCClick(true);
                //}
                return true;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("保存失败！\r\n错误信息："+ex.Message,"保存提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return false;
            }            
            
        }

        private void statetxt0_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            var str = e.NewValue.ToString().PadLeft(4, '0');
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c0e").PValue = $"0x{str[0]}{str[1]}";
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c0f").PValue = $"0x{str[2]}{str[3]}";
        }

        private void statetxt1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            var str = e.NewValue.ToString().PadLeft(4, '0');
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c10").PValue = $"0x{str[0]}{str[1]}";
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c11").PValue = $"0x{str[2]}{str[3]}";
        }

        private void statetxt2_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            var str = e.NewValue.ToString().PadLeft(4, '0');
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c12").PValue = $"0x{str[0]}{str[1]}";
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c13").PValue = $"0x{str[2]}{str[3]}";
        }

        private void statetxt3_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            var str = e.NewValue.ToString().PadLeft(4, '0');
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c14").PValue = $"0x{str[0]}{str[1]}";
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c15").PValue = $"0x{str[2]}{str[3]}";
        }

        private void Erangetxt_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            var str = e.NewValue.ToString().PadLeft(2, '0');
            ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c16").PValue = $"0x{str[0]}{str[1]}";
        }
    }
}